package com.pat.example.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ByExamples implements EntryPoint
{
	final Button m_btnLogin = new Button("Login");

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		loginView();
	}
	
	private void loginView()
	{
		final Label lblLogin = new Label("Login details");
		final Grid grid = new Grid(3, 2);
		
		final Label lblName = new Label("Name");
		final Label lblPass = new Label("Password");
		final TextBox txtName = new TextBox();
		final TextBox txtPass = new TextBox();
		
		DeferredCommand.addCommand(new Command()
		{
			public void execute()
			{
				txtName.setFocus(true);
			}
		});
	
		m_btnLogin.addClickHandler(new ClickHandler()
		{
			
			public void onClick(ClickEvent event)
			{
				Window.alert("Clicked");
//				LoginServiceAsync lsAsync = (LoginServiceAsync)GWT.create(LoginService.class);
//				lsAsync.isValidLogin(txtName.getText(), txtPass.getText(), new AsyncCallback()
				G
				{

					public void onFailure(Throwable caught)
					{
						Window.alert("Failed!");
						
					}

					public void onSuccess(Object result)
					{
						Window.alert("OK!!!");
					}
				});
				
			}
		});
		
		m_btnLogin.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				welcomeView();
			}
		});
		
		grid.setWidget(0, 0, lblName);
		grid.setWidget(0, 1, txtName);
		
		grid.setWidget(1, 0, lblPass);
		grid.setWidget(1, 1, txtPass);
		
		grid.setWidget(2, 1, m_btnLogin);
		
		RootPanel.get().add(lblLogin);
		RootPanel.get().add(grid);
	}

	protected void welcomeView()
	{
		RootPanel.get().clear();
		RootPanel.get().add(new Label("Welcome!"));
	}
	
}
